package com.jinlejia.bbs.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.jinlejia.bbs.R;

/**
 * @author 王进 on 2019/3/20.
 */

public class RecordDialogManager {
    private volatile Dialog dialog;
    private Context context;
    public RecordDialogManager(Context context){
        this.context = context;
    }
    private ImageView voiceImageView,iconImageView;
    private TextView labelTextView;
    private Dialog createDialog(){
        if (dialog==null){
            dialog = new Dialog(context, R.style.BBSThemeAudioDialog);
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.bbs_dialog_record,null);
            dialog.setContentView(view);
            Window window = dialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable());
            iconImageView = dialog.findViewById(R.id.iconImageView);
            voiceImageView = dialog.findViewById(R.id.voiceImageView);
            labelTextView = dialog.findViewById(R.id.labelTextView);
        }
        return dialog;
    }
    private boolean isdismiss;
    private void show(){
        isdismiss = false;
        Dialog dialog = createDialog();
        dialog.show();
    }

    private void dismiss(){
        isdismiss = true;
        Dialog dialog = createDialog();
        dialog.dismiss();
        this.dialog = null;
    }

    public void dismissDialog(){
        dismiss();
    }

    public boolean isShow(){
        if (dialog==null)return false;
        return dialog.isShowing();
    }

    private int[]voiceSrc = new int[]{R.mipmap.bbs_v1,R.mipmap.bbs_v2,R.mipmap.bbs_v3,R.mipmap.bbs_v4,R.mipmap.bbs_v5,R.mipmap.bbs_v6,R.mipmap.bbs_v7};
    public void updateVoiceVolume(int leave){
        if (isdismiss)return;
        int i = (int) Math.ceil((7 * leave) / 2000);
        if (dialog != null&&dialog.isShowing()) {
            if (i>=7)i=6;
            voiceImageView.setImageResource(voiceSrc[i]);
        }
    }

    //想要取消
    public void wantToCancel() {
        show();
        if (dialog != null && dialog.isShowing()) {
            iconImageView.setVisibility(View.VISIBLE);
            voiceImageView.setVisibility(View.GONE);
            labelTextView.setVisibility(View.VISIBLE);
            labelTextView.setBackgroundColor(Color.parseColor("#ffff4444"));
            iconImageView.setImageResource(R.mipmap.bbs_cancel);
            labelTextView.setText("松开手指，取消发送");
        }
    }

    public void recording() {
        show();
        if (dialog != null && dialog.isShowing()) {
            iconImageView.setVisibility(View.VISIBLE);
            voiceImageView.setVisibility(View.VISIBLE);
            labelTextView.setVisibility(View.VISIBLE);
            labelTextView.setBackgroundColor(Color.parseColor("#00000000"));
            iconImageView.setImageResource(R.mipmap.bbs_recorder);
            labelTextView.setText("手指上划，取消发送");
        }
    }

    public void tooShort(){
        show();
        if (dialog != null&&dialog.isShowing()) {
            iconImageView.setVisibility(View.VISIBLE);
            voiceImageView.setVisibility(View.GONE);
            labelTextView.setVisibility(View.VISIBLE);

            iconImageView.setImageResource(R.mipmap.bbs_voice_to_short);
            labelTextView.setBackgroundColor(Color.parseColor("#ffff4444"));
            labelTextView.setText("录音时间过短");
        }
    }


}
